<template>
    <div id="parts">
        <el-button @click="insertList" class="slot-btn-top" type="primary" size="small">添加出库单</el-button>
        <el-table :data="listdata.slice((currentPage-1)*pagesize,currentPage*pagesize)" stripe border :height="tableHeight" :header-cell-style="getRowClass">
            <el-table-column fixed type="index" label="*" sortable min-width="40"></el-table-column>
            <el-table-column prop="hospitalName" label="医院名称" sortable min-width="220"></el-table-column>
            <el-table-column prop="departmentName" label="科室名称" sortable min-width="150"></el-table-column>
            <el-table-column prop="productName" label="产品名称" sortable min-width="200"></el-table-column>
            <el-table-column prop="num" label="数量" sortable min-width="110"></el-table-column>
            <el-table-column prop="orderTime" label="下单时间" sortable min-width="170"></el-table-column>
            <el-table-column prop="outStorageTime" label="出库时间" sortable min-width="170"></el-table-column>
            <el-table-column prop="outStoragePersonName" label="出库人" sortable min-width="110"></el-table-column>
            <el-table-column prop="dealerName" label="经销商" sortable min-width="280"></el-table-column>
        </el-table>
        <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[5,10, 15,20, 25,30,50, 100]" :page-size="pagesize" layout="total, sizes, prev, pager, next, jumper" :total="listdata.length"></el-pagination>


        <el-dialog :title="dialogTitle" width="750px" center :visible.sync="isDialog" :close-on-click-modal="false">
            <el-form :inline="true" class="demo-form-inline" v-model="insertData" label-position="right" label-width="100px">
                <el-form-item label="产品名称"><el-select v-model="insertData.product" filterable allow-create default-first-option placeholder="请输入产品名称"><el-option v-for="item in product" :key="item.id" :label="item.name" :value="item.id"></el-option></el-select></el-form-item>
                <el-form-item label="出库数量"><el-input v-model="insertData.num"></el-input></el-form-item>
                <el-form-item label="医院名称"><el-select v-model="insertData.hospital" filterable allow-create default-first-option placeholder="请输入医院名称"><el-option v-for="item in hospitalName" :key="item.id" :label="item.hospitalName" :value="item.id"></el-option></el-select></el-form-item>
                <el-form-item label="医院科室"><el-select v-model="insertData.department" filterable allow-create default-first-option placeholder="请输入医院科室名称"><el-option v-for="item in applyOffice" :key="item.code" :label="item.name" :value="item.code"></el-option></el-select></el-form-item>
                <el-form-item label="下单时间"><el-date-picker v-model="insertData.orderTime" format="yyyy-MM-dd HH:ss:mm" type="datetime" placeholder="选择日期时间" style="width: 200px;"></el-date-picker></el-form-item>
                <el-form-item label="出库人"><el-input v-model="insertData.outStoragePerson" disabled></el-input></el-form-item>
                <el-form-item label="出库时间"><el-input v-model="insertData.outStorageTime" disabled></el-input></el-form-item>
            </el-form><div slot="footer" class="dialog-footer" style="text-align: center;"><el-button @click="submitList" type="primary">确 定</el-button></div>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        data() {
            let token = sessionStorage.getItem("tokenId");
            return {
                tokenId: token,
                currentPage: 1,
                pagesize: 10,
                tableHeight: document.documentElement.clientHeight - 160,

                listdata:[],
                isDialog:false,
                dialogTitle:"",
                hospitalName:[],
                applyOffice:[],
                product:[],
                insertData:{
                    dealer: "",
                    dealerName: "",
                    department: "",
                    departmentName: "",
                    hospital: "",
                    hospitalName: "",
                    id: "",
                    num: "",
                    orderTime: "",
                    outStoragePerson: "",
                    outStoragePersonName: "",
                    outStorageTime: "",
                    product: "",
                    productName: ""
                }
            };
        },
        created() {
            this.getProduceData();
            this.queryAllHospitalBaseList();
            this.hospital_office();
            this.selectProduct();
        },
        methods: {
            //查询产品名称
            selectProduct() {
                let _this = this;
                this.$http({
                    url: "/api/adscm/stock/jyOutStorage/selectProduct?tokenId=" + _this.tokenId,
                    method: "post",
                    headers: { "X-Requested-With": "XMLHttpRequest" }
                }).then(function(res) {
                    if (res.data.statusCode == 200) {
                        _this.product = res.data.content;
                        console.log(_this.prduct);
                    } else {
                        _this.$message.error(res.data.desc);
                    }
                }).catch(function(catchres) {
                    _this.$message.error("加载失败！请重新加载...");
                });
            },
            // 查询医院名称
            queryAllHospitalBaseList() {
                let _this = this;
                this.$http({
                    url: "/api/adscm/dealer/jyHospitalBase/queryAllHospitalList?tokenId=" + _this.tokenId,
                    method: "post",
                    headers: { "X-Requested-With": "XMLHttpRequest" }
                }).then(function(res) {
                    if (res.data.statusCode == 200) {
                        _this.hospitalName = res.data.content;
                        console.log(_this.listdata);
                    } else {
                        _this.$message.error(res.data.desc);
                    }
                }).catch(function(catchres) {
                    _this.$message.error("加载失败！请重新加载...");
                });
            },
            // 查询医院科室
            hospital_office() {
                let _this = this;
                this.$http({
                    url: "/api/adscm/system/code/queryCoedByType/hospital_office?tokenId=" + _this.tokenId,
                    method: "post",
                    headers: { "X-Requested-With": "XMLHttpRequest" }
                }).then(function(res) {
                    if (res.data.statusCode == 200) {
                        _this.applyOffice = res.data.content;
                        console.log(_this.listdata);
                    } else {
                        _this.$message.error(res.data.desc);
                    }
                }).catch(function(catchres) {
                    _this.$message.error("加载失败！请重新加载...");
                });
            },
            getProduceData() {
                let _this = this;
                this.$http({
                    url: '/api/adscm/stock/jyOutStorage/selectAll?tokenId=' + _this.tokenId,
                    method: 'post',
                    headers: {'X-Requested-With': 'XMLHttpRequest'},
                }).then(function (res) {
                    if (res.data.statusCode == 200) {
                        _this.listdata = res.data.content;
                        console.log(_this.listdata)
                    } else {
                        _this.$message.error(res.data.desc);
                    }
                }).catch(function (catchres) {
                    _this.$message.error("加载失败！请重新加载...");
                })
            },
            insertList() {
                this.isDialog = true;
                this.dialogTitle = "产品出库";
                this.insertData = {
                    dealer: "",
                    dealerName: "",
                    department: "",
                    departmentName: "",
                    hospital: "",
                    hospitalName: "",
                    id: "",
                    num: "",
                    orderTime: "",
                    outStoragePerson: "",
                    outStoragePersonName: "",
                    outStorageTime: "",
                    product: "",
                    productName: ""
                }
            },
            submitList() {
                let _this = this;
                let url;
                if (this.dialogTitle === "产品出库") {
                    url = "/api/adscm/stock/jyOutStorage/insert?tokenId=" + _this.tokenId;
                }
                _this.insertData.orderTime=_this.timestampToTime(_this.insertData.orderTime);

                this.$http({
                    url: url,
                    method: "post",
                    headers: { "X-Requested-With": "XMLHttpRequest" },
                    data: _this.insertData
                }).then(function(res) {
                    if (res.data.statusCode == 200) {
                        _this.isDialog = false;
                        _this.getProduceData()
                    }
                }).catch(function(catchres) {
                    _this.$message.error("加载失败！请重新加载...");
                });
            },

            handleSizeChange: function (size) {
                this.pagesize = size;
            },
            handleCurrentChange: function (currentPage) {
                this.currentPage = currentPage;
            },

            timestampToTime(timestamp) {
                if (timestamp) {
                    let date = new Date(timestamp);
                    let Y = date.getFullYear() + "-";
                    let M =
                        (date.getMonth() + 1 < 10
                            ? "0" + (date.getMonth() + 1)
                            : date.getMonth() + 1) + "-";
                    let D =
                        (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";
                    let h =
                        (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) +
                        ":";
                    let m =
                        (date.getMinutes() < 10
                            ? "0" + date.getMinutes()
                            : date.getMinutes()) + ":";
                    return timestamp ? Y + M + D + h + m + "00" : "";
                } else {
                    return "";
                }
            }
        }


    };
</script>

<style  scoped>

</style>
